package com.jdew.thkj.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.ali.auth.third.core.MemberSDK;
import com.ali.auth.third.core.callback.InitResultCallback;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.jdew.thkj.bean.UserBean;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.kepler.jd.Listener.AsyncInitListener;
import com.kepler.jd.login.KeplerApiManager;
import com.kk.taurus.playerbase.config.PlayerLibrary;
import com.pgyersdk.crash.PgyCrashManager;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.stat.StatConfig;
import com.tencent.stat.StatService;

import java.util.Calendar;

import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by yangyang on 2017/9/8.
 */

public class MyApplaction extends Application {
    private static MyApplaction instance;
    public static UserBean user;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        MemberSDK.init(this, new InitResultCallback() {

            @Override
            public void onFailure(int code, String msg) {
                Log.e("onFailure", "onFailure:" + code + "," + msg);
            }

            @Override
            public void onSuccess() {
                Log.e("onFailure", "onSuccess:");
            }
        });
        KeplerApiManager.asyncInitSdk(MyApplaction.this, "681add9ae7114ef8b32ddd8047f7cc4c", "5b893a8ca7744275bc602915fecd0eb2",
                new AsyncInitListener() {
                    @Override
                    public void onSuccess() {
                        Log.e("Kepler", "Kepler asyncInitSdk onSuccess ");
                    }

                    @Override
                    public void onFailure() {
                        Log.e("Kepler", "Kepler asyncInitSdk 授权失败，请检查 lib 工程资源引用；包名,签名证书是否和注册一致");
                    }
                });
//        BGASwipeBackHelper.init(this, null);
        StatConfig.setDebugEnable(true);
        // 基础统计API
        //初始化库
//        PlayerLibrary.init(this);
        StatService.registerActivityLifecycleCallbacks(this);
        String alias = sp.getString("Alias", "");
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        PgyCrashManager.register(this);
        int sequence = getSequence();
        editor.putInt("sequence", sequence);
        editor.apply();
        JPushInterface.setAlias(this, sequence, alias);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                //初始化成功，设置相关的全局配置参数
                // ...
            }

            @Override
            public void onFailure(int code, String msg) {
                //初始化失败，可以根据code和msg判断失败原因，详情参见错误说明
            }
        });
        initX5WebView();
    }

    private void initX5WebView() {
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }


    ///实例化一次
    public synchronized static MyApplaction getInstance() {
        if (null == instance) {
            instance = new MyApplaction();
        }
        return instance;
    }

    //杀进程
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        RegexValidateUtils.fix();
    }

    public static final int getSequence() {
        int result = getYear();
        result = 31 * result + getMonth();
        result = 31 * result + getDay();
        return result;
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

}